﻿
Partial Class WebForm_ManageMOIExport_WorkHourControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Me.BindGV()
        End If
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As New Data.DataTable

        sql.Length = 0

        sql.AppendFormat(" Select Distinct v.VolunteerName,v.VolunteerID ")
        sql.AppendFormat(",right('0'+CAST((YEAR(A.WorkStartDate)-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),A.WorkStartDate,112),4),7) AS WorkStartDate ")
        sql.AppendFormat(",right('0'+CAST((YEAR(A.WorkEndDate)-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),A.WorkEndDate,112),4),7) AS WorkEndDate, ")
        sql.AppendFormat(" '0340' AS WorkItem,'0099' AS WorkContent,C.WorkHour,'0' AS NumPeople, ")
        sql.AppendFormat(" '0' AS TCharge,'0' AS ECharge,'F' as SArea,'' AS Remark  ")
        sql.AppendFormat(" From WorkHeader A Inner Join WorkRecord B ON A.WorkHeaderID=B.WorkHeaderID ")
        sql.AppendFormat(" Inner Join WorkHourRecord C ON B.RecordID=C.RecordID ")
        sql.AppendFormat(" Inner Join Volunteer v ON C.VolunteerID = v.VolunteerID ")
        'sql.AppendFormat(" Inner Join CleanPlanMapping cpm ON A.WorkHeaderID = cpm.WorkHeaderID ")
        'sql.AppendFormat(" Inner Join CleanPlan cp ON cpm.PlanID = cp.PlanID ")
        '  sql.AppendFormat(" Left Join VillagePeopleNUM vpn ON vpn.RegionID=cp.VillageID ")
        ' sql.AppendFormat(" Left Join Village vi ON v.VillageID = vi.RegionID ")
        sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")
        sql.AppendFormat(" Where 1=1   and A.Active='Y' and C.WorkHour<>0 ")

        'If Me.Region1ID <> "" Then
        '    sql.AppendFormat(" And vi.Parent='{0}' ", Me.Region1ID)
        'End If

        'If Me.Region2ID <> "" Then
        '    sql.AppendFormat(" And v.VillageID='{0}' ", Me.Region2ID)
        'End If

        If Me.OrganizationID <> "0" Then
            sql.AppendFormat("and oo.DirectUOrganizationID='{0}' ", Me.OrganizationID)
        End If

        If Me.VOrganizationID <> "0" Then
            sql.AppendFormat(" And oo.VOrganizationID='{0}' ", Me.VOrganizationID)
        End If

        If Me.VolunteerID <> "" Then
            sql.AppendFormat(" And v.VolunteerID='{0}' ", Me.VolunteerID)
        End If

        If Me.VolunteerName <> "" Then
            sql.AppendFormat(" And v.VolunteerName Like '%{0}%' ", Me.VolunteerName)
        End If

        sql.AppendFormat(" Union ALL ")
        sql.AppendFormat(" Select v.VolunteerName,v.VolunteerID, ")
        sql.AppendFormat("right('0'+CAST((YEAR(A.StartDate)-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),A.StartDate,112),4),7) AS StartDate ")
        sql.AppendFormat(",right('0'+CAST((YEAR(A.EffectiveDate)-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),A.EffectiveDate,112),4),7) AS EffectiveDate ")
        sql.AppendFormat(",'0340','0099',SUM(A.WorkHour),'0','0','0','F','' ")
        sql.AppendFormat(" From ImportWorkHourRecord A Inner Join Volunteer v ON A.VolunteerID = v.VolunteerID ")
        ' sql.AppendFormat(" Left Join Village vi ON v.VillageID = vi.RegionID ")
        sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")
        sql.AppendFormat(" Where 1=1 and A.WorkHour<>0 ")

        If Me.OrganizationID <> "0" Then
            sql.AppendFormat("and oo.DirectUOrganizationID='{0}' ", Me.OrganizationID)
        End If

        If Me.VOrganizationID <> "0" Then
            sql.AppendFormat(" And oo.VOrganizationID='{0}' ", Me.VOrganizationID)
        End If

        If Me.VolunteerID <> "" Then
            sql.AppendFormat(" And v.VolunteerID='{0}' ", Me.VolunteerID)
        End If

        If Me.VolunteerName <> "" Then
            sql.AppendFormat(" And v.VolunteerName Like '%{0}%' ", Me.VolunteerName)
        End If

        sql.AppendFormat(" Group BY v.VolunteerName,v.VolunteerID,A.StartDate,A.EffectiveDate ")

        WorkHourDT = dat.GetData(sql)

        Me.gv.DataSource = WorkHourDT.DefaultView
        Me.gv.DataBind()

    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub

    'Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
    '    If e.Row.RowType = DataControlRowType.DataRow Then

    '        If CType(e.Row.FindControl("lblWorkStartDate"), Label).Text <> "" Then
    '            Dim d() As String = CType(e.Row.FindControl("lblWorkStartDate"), Label).Text.Split(".")     'year=d(0),month=d(1),day=d(2)

    '            '日期格式 099/01/01
    '            If d(0).Length = 2 Then
    '                d(0) = "0" + d(0)
    '            End If

    '            If d(1).Length = 1 Then
    '                d(1) = "0" + d(1)
    '            End If

    '            If d(2).Length = 1 Then
    '                d(2) = "0" + d(2)
    '            End If

    '            CType(e.Row.FindControl("lblWorkStartDate"), Label).Text = d(0) + "/" + d(1) + "/" + d(2)
    '        End If

    '        If CType(e.Row.FindControl("lblWorkEndDate"), Label).Text <> "" Then
    '            Dim d() As String = CType(e.Row.FindControl("lblWorkEndDate"), Label).Text.Split(".")     'year=d(0),month=d(1),day=d(2)

    '            '日期格式 099/01/01
    '            If d(0).Length = 2 Then
    '                d(0) = "0" + d(0)
    '            End If

    '            If d(1).Length = 1 Then
    '                d(1) = "0" + d(1)
    '            End If

    '            If d(2).Length = 1 Then
    '                d(2) = "0" + d(2)
    '            End If

    '            CType(e.Row.FindControl("lblWorkEndDate"), Label).Text = d(0) + "/" + d(1) + "/" + d(2)
    '        End If




    '    End If
    'End Sub


#Region "Property"
    Public Property WorkHourDT() As Data.DataTable
        Get
            Return ViewState("WorkHourDT")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("WorkHourDT") = value
        End Set
    End Property
    Public Property Region1ID() As String
        Get
            Return ViewState("Region1ID")
        End Get
        Set(ByVal value As String)
            ViewState("Region1ID") = value
        End Set
    End Property

    Public Property Region2ID() As String
        Get
            Return ViewState("Region2ID")
        End Get
        Set(ByVal value As String)
            ViewState("Region2ID") = value
        End Set
    End Property
    Public Property OrganizationID() As String
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("OrganizationID") = value
        End Set
    End Property
    Public Property VOrganizationID() As String
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set
    End Property

    Public Property VolunteerName() As String
        Get
            Return ViewState("VolunteerName")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerName") = value
        End Set
    End Property

    Public Property VolunteerID() As String
        Get
            Return ViewState("VolunteerID")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerID") = value
        End Set
    End Property
#End Region


End Class
